package cn.hn3l.agriculture.controllers.manager.goods;

import cn.hn3l.agriculture.dao.mapper.file.PictureMapper;
import cn.hn3l.agriculture.services.interfaces.goods.ProviderService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by weiwei02 on 2016/10/19.
 */
@Controller
@RequestMapping("/manager/goods/productors")
public class ManagerGoodsProductorController {
    @Resource
    private PictureMapper pictureMapper;
    @Resource
    private ProviderService providerService;
    @RequestMapping("/list")
    public String listProductors( Model model){
        model.addAttribute("title","生产厂商列表");
        model.addAttribute("list",providerService.getProductorsOfPage(0));
        return "manager/goods/productors_list_content";
    }

    @ResponseBody
    @RequestMapping(value = "/detail/{productorId}",
                    produces = "application/json;charset=UTF-8")
    public String detailProductor(@PathVariable int productorId){
        return JSON.toJSONString(providerService.getProductorById(productorId));
    }

    @ResponseBody
    @RequestMapping(value = "/detail/goods_id/{goodsId}",
            produces = "application/json;charset=UTF-8")
    public String detailProductorByGoodsId(@PathVariable int goodsId){
        return JSON.toJSONString(providerService.getProductorById(goodsId));
    }

    @RequestMapping(value = "",
            produces = "application/json;charset=UTF-8")
    public void picture(HttpServletResponse response) throws IOException {
        //response.setContentType("image/jpeg")
        // ;
        try {
            byte[] baniry = pictureMapper.findStreamById(1).getFile();
            response.setContentType("image/jpeg;charset=utf-8");
            response.getOutputStream().write(baniry);
            response.flushBuffer();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
